const medicalService = require('../../../services/medical');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    petId: null,
    petName: '',
    form: {
      vaccineName: '',
      vaccineTime: '',
      nextTime: '',
      status: 0,
      remark: ''
    },
    statusOptions: [
      { id: 0, name: '未接种' },
      { id: 1, name: '已接种' },
      { id: 2, name: '已过期' }
    ],
    isSubmitting: false,
    today: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 设置今天日期，用于日期选择的最小值
    const today = new Date();
    const todayStr = today.toISOString().split('T')[0];
    
    this.setData({
      today: todayStr,
      ['form.vaccineTime']: todayStr
    });
    
    if (options.petId) {
      this.setData({
        petId: options.petId
      });
      
      // 获取宠物名称
      const petInfo = wx.getStorageSync('currentPet');
      if (petInfo && petInfo.id == options.petId) {
        this.setData({
          petName: petInfo.name
        });
        wx.setNavigationBarTitle({
          title: `为${petInfo.name}添加疫苗记录`
        });
      }
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  /**
   * 输入疫苗名称
   */
  inputVaccineName(e) {
    this.setData({
      ['form.vaccineName']: e.detail.value
    });
  },

  /**
   * 选择接种日期
   */
  selectVaccineTime(e) {
    this.setData({
      ['form.vaccineTime']: e.detail.value
    });
  },

  /**
   * 选择下次接种日期
   */
  selectNextTime(e) {
    this.setData({
      ['form.nextTime']: e.detail.value
    });
  },

  /**
   * 选择状态
   */
  selectStatus(e) {
    this.setData({
      ['form.status']: parseInt(e.detail.value)
    });
  },

  /**
   * 输入备注
   */
  inputRemark(e) {
    this.setData({
      ['form.remark']: e.detail.value
    });
  },

  /**
   * 提交表单
   */
  submitForm() {
    // 表单验证
    if (!this.data.form.vaccineName) {
      wx.showToast({
        title: '请输入疫苗名称',
        icon: 'none'
      });
      return;
    }

    if (!this.data.form.vaccineTime) {
      wx.showToast({
        title: '请选择接种日期',
        icon: 'none'
      });
      return;
    }

    if (this.data.isSubmitting) {
      return;
    }

    this.setData({
      isSubmitting: true
    });

    // 组装数据
    const data = {
      petId: this.data.petId,
      vaccineName: this.data.form.vaccineName,
      vaccineTime: this.data.form.vaccineTime,
      nextTime: this.data.form.nextTime || null,
      status: this.data.form.status,
      remark: this.data.form.remark
    };

    // 提交数据
    medicalService.addVaccineRecord(data)
      .then(res => {
        wx.showToast({
          title: '添加成功',
          icon: 'success'
        });

        // 返回上一页
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      })
      .catch(err => {
        wx.showToast({
          title: '添加失败，请重试',
          icon: 'none'
        });
      })
      .finally(() => {
        this.setData({
          isSubmitting: false
        });
      });
  }
}); 